/**
 * @Project Name:blog-login-api
 * @Package Name:cn.hoofungson.blog.login.api.controller.oauth2
 * @Since JDK 1.8
 */

package cn.hoofungson.blog.login.api.controller.oauth2;

import cn.hoofungson.blog.login.api.domain.dto.oauth2.JwtCheckTransportDTO;
import cn.hoofungson.blog.login.api.domain.dto.oauth2.JwtTransportDTO;
import cn.hoofungson.blog.login.api.domain.vo.oauth2.OAuth2CheckVO;
import cn.hoofungson.blog.login.api.domain.vo.oauth2.OAuth2UserVO;
import cn.hoofungson.blog.login.api.service.oauth2.OAuth2Service;
import cn.hoofungson.blog.login.api.util.RandomValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: LoginApiController
 * @Author 胡松 hoofungson@163.com
 * @Date 2020-03-26 14:09
 * @Version V1.0
 */
@Slf4j
@RequestMapping("/rest/api/auth")
@RestController
public class OAuth2ApiController {

    @Autowired
    private OAuth2Service oAuth2Service;

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     **/
    @PostMapping(value = "/login")
    public HttpEntity<Map<String, Object>> login(
            @RequestParam(name = "username") String username
            , @RequestParam(name = "password") String password) {
        JwtTransportDTO jwtTransportDTO = this.oAuth2Service.auth(username, password);
        OAuth2UserVO oAuth2UserVO = new OAuth2UserVO();
        oAuth2UserVO.setToken(jwtTransportDTO.getAccessToken());
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("code", 20000);
        map.put("data", oAuth2UserVO);
        map.put("message", "登录成功！！");
        return new HttpEntity<>(map);
    }

    /**
     * 获取当前登录用户信息
     *
     * @param token
     * @return
     **/
    @PostMapping(value = "/login/info")
    public HttpEntity<Map<String, Object>> loginInfo(@RequestParam(name = "token") String token) {
        JwtCheckTransportDTO jwtCheckTransportDTO = this.oAuth2Service.checkToken(token);
        OAuth2CheckVO oAuth2CheckVO = new OAuth2CheckVO();
        oAuth2CheckVO.setName(jwtCheckTransportDTO.getUsername());
        List<String> roles = new ArrayList<>();
        roles.add("admin");
        oAuth2CheckVO.setRoles(roles);
        oAuth2CheckVO.setIntroduction("这是一个测试账户！！");
        oAuth2CheckVO.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("code", 20000);
        map.put("data", oAuth2CheckVO);
        map.put("message", "获取当前用户成功！！");
        return new HttpEntity<>(map);
    }

    /**
     * 用户退出登录
     *
     * @return
     **/
    @PostMapping(value = "/logout")
    public HttpEntity<Map<String, Object>> logout() {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("code", 20000);
        map.put("data", "success");
        map.put("message", "退出成功！！");
        return new HttpEntity<>(map);
    }

    /**
     * 生成验证码
     *
     * @param request
     * @param response
     */
    @GetMapping(value = "/verify/code/image")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCodeUtils randomValidateCode = new RandomValidateCodeUtils();
        randomValidateCode.getRandCode(request, response);//输出验证码图片方法
    }
}
